package cn.jhc;

public class Scorer {
    private int[] scores = new int[21];
    private int ball;
    private int current = 0;

    public void addThrow(int pins) {
        scores[current++] = pins;
    }
    
    public int getScoreForFrame(int frame) {
        int score = 0;
        ball = 0;
        for (int i = 0; i < frame; i++) {
            if (strike()) {
                ball++;
                score += 10 + nextTwoBalls();
            } else if (spare()) {
                ball += 2;
                score += 10 + scores[ball];
            } else {
                score += twoBallsInFrame();
                ball += 2;
            }
        }
        return score;
    }

    private boolean strike() {
        return scores[ball] == 10;
    }

    private int nextTwoBalls() {
        return scores[ball] + scores[ball + 1];
    }

    private int twoBallsInFrame() {
        return scores[ball] + scores[ball + 1];
    }

    private boolean spare() {
        return scores[ball] + scores[ball + 1] == 10;
    }
}
